package com.cloudkinto.service.finance.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.appletservice.finance.vo.CostRecordPageRes;
import com.cloudkinto.common.BeanConvert;
import com.cloudkinto.common.DicBusinessConstant;
import com.cloudkinto.common.utils.ExcelUtils;
import com.cloudkinto.common.utils.MathUtils;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.utils.dict.StaticDict;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.extentity.OrderOutputExportDto;
import com.cloudkinto.extentity.StockSnapshotPageDto;
import com.cloudkinto.service.file.FileService;
import com.cloudkinto.service.finance.CostRecordService;
import com.cloudkinto.service.finance.vo.CostRecordListRes;
import com.cloudkinto.service.dic.vo.LanguageValueEntity;
import com.cloudkinto.service.finance.vo.OrderDoorExportVo;
import com.cloudkinto.service.finance.vo.exportVo.*;
import com.cloudkinto.service.orderback.OrderBackService;
import com.cloudkinto.service.orderinput.OrderInputService;
import com.cloudkinto.service.orderinput.vo.InputMarkProduct;
import com.cloudkinto.service.stock.StockRecordService;
import com.cloudkinto.service.stock.StockSnapshotExportVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author ding
 * @since 2020-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CostRecordServiceImpl extends ServiceImpl<CostRecordDao, CostRecordDo> implements CostRecordService {
    @Autowired
    private ShopDao shopDao;
    @Lazy
    @Autowired
    private OrderOutputDao orderOutputDao;
    @Autowired
    private OrderOutputPackageDao orderOutputPackageDao;
    @Autowired
    private OrderOutputProductDao orderOutputProductDao;
    @Autowired
    private StorageDao storageDao;
    @Autowired
    private ProductInfoDao productInfoDao;
    @Autowired
    private OrderInputDao orderInputDao;
    @Autowired
    private OrderInputService orderInputService;
    @Autowired
    private StockHistoryDao stockHistoryDao;
    @Autowired
    private OrderWorkDao orderWorkDao;
    @Autowired
    private OrderWorkPriceDao orderWorkPriceDao;
    @Autowired
    private DicBusinessItemDao dicBusinessItemDao;
    @Autowired
    private FbaOrderOutputDao fbaOrderOutputDao;
    @Autowired
    private FbaStorageDao fbaStorageDao;
    @Autowired
    private FbaOrderOutputPackageDao fbaOrderOutputPackageDao;
    @Autowired
    private FbaOrderOutputProductDao fbaOrderOutputProductDao;
    @Autowired
    private OrderBackDao orderBackDao;
    @Autowired
    private OrderBackProductDao orderBackProductDao;
    @Autowired
    private ReplacementDao replacementDao;
    @Autowired
    private ManageBillDao manageBillDao;
    @Autowired
    private ManageHeaderPriceItemDao manageHeaderPriceItemDao;
    @Autowired
    private StorageServiceProjectDao storageServiceProjectDao;
    @Autowired
    private FileService fileService;
    @Autowired
    private OrderDoorPickDao orderDoorPickDao;
    @Autowired
    private StockRecordDao stockRecordDao;
    @Lazy
    @Autowired
    private StockRecordService stockRecordService;

    @Override
    public Object add(Object req, Long userId) {
        CostRecordDo entityDo = new CostRecordDo();
        BeanUtils.copyProperties(req, entityDo);
        baseMapper.insert(entityDo);
        return handlerAddAfter(entityDo);
    }

    /**
     * @param type      业务类型 0出库订单 1 入库订单 2仓储费 3定制费 4 退货 5 转运单 6头程费 7 维修订单费用
     * @param content
     * @param cost
     * @param shopId
     * @param companyId
     * @param thirdId
     * @param status    0未入账 1已扣款
     * @return
     */
    @Override
    public boolean add(int type, String content, double cost, Long shopId, Long companyId, Long thirdId, Integer status) {
        CostRecordDo costRecordDo = new CostRecordDo();
        costRecordDo.setType(type);
        costRecordDo.setContent(content);
        costRecordDo.setCostAmount(new BigDecimal(cost).setScale(0, RoundingMode.HALF_UP).doubleValue());
        costRecordDo.setShopId(shopId);
        costRecordDo.setCompanyId(companyId + "");
        costRecordDo.setThirdId(thirdId + "");
        costRecordDo.setCreateTime(new Date());
        costRecordDo.setUpdateTime(new Date());
        if (status == 1) {
            costRecordDo.setPayTime(new Date());
        }
        costRecordDo.setStatus(status);
        if (status == 1) {
            handlerAddAfter(costRecordDo);
        }
        int rows = baseMapper.insert(costRecordDo);
        return rows > 0;
    }

    /**
     * 将新增的do转换成要返回的实体
     */
    private Object handlerAddAfter(CostRecordDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public Object update(Object req, Long userId) {
        /*CostRecordDo entityDo = dao.selectById(req.getId());
        BeanUtils.copyProperties(req, entityDo);
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
        return handlerUpdateAfter(entityDo);*/
        return null;
    }

    /**
     * 做修改之前要执行的业务定制处理
     */
    private void handlerUpdateBefore(CostRecordDo entityDo, Long userId) {
//        entityDo.setUpdateBy(userId);
//        entityDo.setUpdateTime(new Date());
    }

    @Override
    public int delete(Long id, Long userId) {
//        CostRecordDo entityDo = dao.selectById(id);
//        handlerDeleteBefore(entityDo, userId);
        return baseMapper.deleteById(id);
    }

    @Override
    public Object detail(Long id) {
        Object result;
        CostRecordDo costRecordDo = baseMapper.selectById(id);
        CostRecordPageRes costRecordRes = new CostRecordPageRes();
        BeanUtils.copyProperties(costRecordDo, costRecordRes);
        costRecordRes.setType(costRecordDo.getType() + "");
        costRecordRes.setStatus(costRecordDo.getStatus() == null || 0 == costRecordDo.getStatus() ? "未入账" : "已扣款");
        ShopDo shopDo = shopDao.selectById(costRecordDo.getShopId());
        if (shopDo != null) {
            costRecordRes.setShop(shopDo.getShopName());
        }
        if (costRecordDo.getType() == 0) {//出库订单
            result = handlerOrderOutPut(costRecordDo, costRecordRes);
        } else if (costRecordDo.getType() == 1) {//入库订单
            result = handlerOrderInput(costRecordDo, costRecordRes);
        } else if (costRecordDo.getType() == 2) {//仓储费
            result = handlerStockFee(costRecordDo, costRecordRes);
        } else if (costRecordDo.getType() == 3) {//定制费
            result = handlerWorkOrder(costRecordDo, costRecordRes);
        } else if (costRecordDo.getType() == 4) {//退货费
            result = handlerBackOrder(costRecordDo, costRecordRes);
        } else if (costRecordDo.getType() == 5) {//FBA转运
            result = handlerFbaOrderOutPut(costRecordDo, costRecordRes);
        } else {
            result = null;
        }
        return result;
    }

    @Override
    public SingleResult pageInit() {
        List<LanguageValueEntity> typeList = new ArrayList<>();
        typeList.add(new LanguageValueEntity(0, "出库费"));
        typeList.add(new LanguageValueEntity(1, "入库费"));
        typeList.add(new LanguageValueEntity(2, "仓储费"));
        typeList.add(new LanguageValueEntity(3, "定制费"));
        typeList.add(new LanguageValueEntity(4, "退货费"));
        typeList.add(new LanguageValueEntity(5, "FBA运费"));
        typeList.add(new LanguageValueEntity(6, "头程运费"));
        typeList.add(new LanguageValueEntity(7, "维修订单"));
        typeList.add(new LanguageValueEntity(8, "上门取件"));
        typeList.add(new LanguageValueEntity(9, "废弃费"));

        return SingleResult.success(typeList);
    }

    @Override
    public List<CostRecordListRes> getList(Map<String, String> map) {
        QueryWrapper<CostRecordDo> wrapper = queryBuild(map);
        List<CostRecordDo> list = this.list(wrapper);
        return handlerListAfter(list);
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<CostRecordDo> wrapper = queryBuild(map);
        IPage<CostRecordDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<CostRecordListRes> resList = handlerListAfter(page.getRecords());
        return new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
    }

    @Override
    public void exportExcel(Map<String, String> map, HttpServletResponse response) throws IOException {
        //出库单导出
        List<CostRecordPageRes> costRecordListRes = new ArrayList<>(); //出库单导出
        List<OrderOutputVo> orderOutExcelExportVos = new ArrayList<>();
        //入库费用
        List<OrderInputExcelExportVo> orderInputExcelExportVos = new ArrayList<>();
        //仓储费
        List<StockFeeExcelExportVo> stockFeeExcelExportVos = new ArrayList<>();
        //工单费用
        List<OrderWorkExcelExportVo> orderWorkExcelExportVos = new ArrayList<>();
        //退货费用
        List<OrderBackExcelExportVo> orderBackExcelExportVos = new ArrayList<>();
        //头程费用
        List<HeaderBillExportVo> headerBillExportVos = new ArrayList<>();
        //上门取件费用
        List<OrderDoorExportVo> orderDoorExportVos = new ArrayList<>();

        QueryWrapper<CostRecordDo> wrapper = queryBuild(map);
        List<CostRecordDo> costRecordDoList = list(wrapper);
        if (!CollectionUtils.isEmpty(costRecordDoList)) {
            // List<String> pjIdCollect = costRecordDoList.stream().filter(costRecordDo -> costRecordDo.getType() == 0
            //         && costRecordDo.getContent().startsWith("PJ")).map(CostRecordDo::getThirdId).collect(Collectors.toList());
            Map<String,Double> pjIdList = costRecordDoList.stream().filter(costRecordDo -> costRecordDo.getContent().startsWith("PJ"))
                    .collect(Collectors.toMap(CostRecordDo::getThirdId, CostRecordDo::getCostAmount, (aDouble, aDouble2) -> aDouble+aDouble2));
            List<String> outIdCollect = costRecordDoList.stream().filter(costRecordDo -> costRecordDo.getType() == 0 && costRecordDo.getContent().startsWith("OT")).map(CostRecordDo::getThirdId).collect(Collectors.toList());
            List<String> fbaIdCollect = costRecordDoList.stream().filter(costRecordDo -> costRecordDo.getType() == 5).map(CostRecordDo::getThirdId).collect(Collectors.toList());

            List<OrderOutputVo> outputExcelExportVos = handlerOrderOutPutNew(outIdCollect, pjIdList, fbaIdCollect);
            List<ShopDo> shopDoList = shopDao.selectList(null);
            Map<Long, ShopDo> shopDoMap = null;
            if (!CollectionUtils.isEmpty(shopDoList)) {
                shopDoMap = shopDoList.stream().collect(Collectors.toMap(ShopDo::getId, shopDo -> shopDo));
            }
            orderOutExcelExportVos.addAll(outputExcelExportVos);
            for (CostRecordDo costRecordDo : costRecordDoList) {
                CostRecordPageRes costRecordRes = new CostRecordPageRes();
                BeanUtils.copyProperties(costRecordDo, costRecordRes);
                costRecordRes.setType(getTypeMap().get(costRecordDo.getType()));
                costRecordRes.setStatus(costRecordDo.getStatus() == null || 0 == costRecordDo.getStatus() ? "未入账" : "已扣款");
                if (shopDoMap != null) {
                    ShopDo shopDo = shopDoMap.get(costRecordDo.getShopId());
                    if (shopDo != null) {
                        costRecordRes.setShop(shopDo.getShopName());
                    }
                }
                if (costRecordDo.getType() == 1) {//入库订单
                    OrderInputExcelExportVo excelExportVo = handlerOrderInput(costRecordDo, costRecordRes);
                    if (excelExportVo != null) {
                        orderInputExcelExportVos.add(excelExportVo);
                    }
                } else if (costRecordDo.getType() == 2) {//仓储费
                    StockFeeExcelExportVo excelExportVo = handlerStockFee(costRecordDo, costRecordRes);
                    if (excelExportVo != null) {
                        stockFeeExcelExportVos.add(excelExportVo);
                    }
                } else if (costRecordDo.getType() == 3) {//定制费
                    OrderWorkExcelExportVo excelExportVo = handlerWorkOrder(costRecordDo, costRecordRes);
                    if (excelExportVo != null) {
                        orderWorkExcelExportVos.add(excelExportVo);
                    }
                } else if (costRecordDo.getType() == 4) {//退货费
                    OrderBackExcelExportVo excelExportVo = handlerBackOrder(costRecordDo, costRecordRes);
                    if (excelExportVo != null) {
                        orderBackExcelExportVos.add(excelExportVo);
                    }
                } else if (costRecordDo.getType() == 5) {//FBA转运
//                    OrderOutputVo excelExportVo = handlerOrderOutPutNew();
//                    if (excelExportVo != null) {
//                        orderOutExcelExportVos.add(excelExportVo);
//                    }
                } else if (costRecordDo.getType() == 6) { //头程费用
                    HeaderBillExportVo exportVo = this.handlerHeaderFee(Long.parseLong(costRecordDo.getThirdId()), costRecordRes);
                    if (exportVo != null) {
                        exportVo.setBillName(costRecordDo.getContent());
                        exportVo.setStatus(costRecordDo.getStatus());
                        exportVo.setCostAmount(costRecordDo.getCostAmount());
                        headerBillExportVos.add(exportVo);
                    }
                } else if (costRecordDo.getType() == 8) {
                    //上门取件费用
                    OrderDoorExportVo orderDoorExportVo = this.handlerOrderDoor(costRecordDo);
                    if (orderDoorExportVo != null) {
                        orderDoorExportVos.add(orderDoorExportVo);
                    }
                }

                costRecordListRes.add(costRecordRes);
            }
        }

        // 创建参数对象（用来设定excel得sheet得内容等信息）
        ExportParams orderCommonParams = new ExportParams();
        // 设置sheet得名称
        orderCommonParams.setSheetName("总计");
        // 创建sheet1使用得map
        Map<String, Object> orderCommonMap = new HashMap<>();
        // title的参数为ExportParams类型，目前仅仅在ExportParams中设置了sheetName
        orderCommonMap.put("title", orderCommonParams);
        // 模版导出对应得实体类型
        orderCommonMap.put("entity", CostRecordPageRes.class);
        // sheet中要填充得数据
        orderCommonMap.put("data", costRecordListRes);

        // 创建参数对象（用来设定excel得sheet得内容等信息）
        ExportParams orderOutputParams = new ExportParams();
        // 设置sheet得名称
        orderOutputParams.setSheetName("出库费用");
        // 创建sheet1使用得map
        Map<String, Object> orderOutputMap = new HashMap<>();
        // title的参数为ExportParams类型，目前仅仅在ExportParams中设置了sheetName
        orderOutputMap.put("title", orderOutputParams);
        // 模版导出对应得实体类型
        orderOutputMap.put("entity", OrderOutputVo.class);
        // sheet中要填充得数据
        orderOutputMap.put("data", orderOutExcelExportVos);


        ExportParams orderInputParams = new ExportParams();
        orderInputParams.setSheetName("入库费用");
        // 创建sheet2使用得map
        Map<String, Object> orderInputMap = new HashMap<>();
        orderInputMap.put("title", orderInputParams);
        orderInputMap.put("entity", OrderInputExcelExportVo.class);
        orderInputMap.put("data", orderInputExcelExportVos);


        ExportParams stockFeeParams = new ExportParams();
        stockFeeParams.setSheetName("仓储费用");
        // 创建sheet3使用得map
        Map<String, Object> stockFeeMap = new HashMap<>();
        stockFeeMap.put("title", stockFeeParams);
        stockFeeMap.put("entity", StockFeeExcelExportVo.class);
        stockFeeMap.put("data", stockFeeExcelExportVos);


        map.put("startDate", map.get("dateStart"));
        map.put("endDate", map.get("dateEnd"));
        List<StockSnapshotExportVo> stockSnapExportVoList = stockRecordService.getStockSnapExportList(map);

        ExportParams stockFeeDetailParams = new ExportParams();
        stockFeeDetailParams.setSheetName("仓储明细费用");
        // 创建sheet3使用得map
        Map<String, Object> stockFeeDetailMap = new HashMap<>();
        stockFeeDetailMap.put("title", stockFeeDetailParams);
        stockFeeDetailMap.put("entity", StockSnapshotExportVo.class);
        stockFeeDetailMap.put("data", stockSnapExportVoList);



        ExportParams orderWorkParams = new ExportParams();
        orderWorkParams.setSheetName("工单费用");
        // 创建sheet4使用得map
        Map<String, Object> orderWorkMap = new HashMap<>();
        orderWorkMap.put("title", orderWorkParams);
        orderWorkMap.put("entity", OrderWorkExcelExportVo.class);
        orderWorkMap.put("data", orderWorkExcelExportVos);

        ExportParams orderBackParams = new ExportParams();
        orderBackParams.setSheetName("退货费用");
        // 创建sheet4使用得map
        Map<String, Object> orderBackMap = new HashMap<>();
        orderBackMap.put("title", orderBackParams);
        orderBackMap.put("entity", OrderBackExcelExportVo.class);
        orderBackMap.put("data", orderBackExcelExportVos);

        ExportParams headerParams = new ExportParams();
        headerParams.setSheetName("头程费用");
        // 创建sheet4使用得map
        Map<String, Object> headerMap = new HashMap<>();
        headerMap.put("title", headerParams);
        headerMap.put("entity", HeaderBillExportVo.class);
        headerMap.put("data", headerBillExportVos);

        ExportParams orderDoorParams = new ExportParams();
        orderDoorParams.setSheetName("上门取件");
        // 创建sheet4使用得map
        Map<String, Object> orderDoorMap = new HashMap<>();
        orderDoorMap.put("title", orderDoorParams);
        orderDoorMap.put("entity", OrderDoorExportVo.class);
        orderDoorMap.put("data", orderDoorExportVos);

        // 将sheet1、sheet2、sheet3、sheet4使用得map进行包装
        List<Map<String, Object>> sheetsList = new ArrayList<>();
        sheetsList.add(orderCommonMap);
        sheetsList.add(orderOutputMap);
        sheetsList.add(orderInputMap);
        sheetsList.add(stockFeeMap);
        sheetsList.add(stockFeeDetailMap);
        sheetsList.add(orderWorkMap);
        sheetsList.add(orderBackMap);
        sheetsList.add(headerMap);
        sheetsList.add(orderDoorMap);

        ExcelUtils.exportExcel(sheetsList, "费用明细", response);
    }

    private Map<Long, DicBusinessItemDo> getTracIdMap() {
        LambdaQueryWrapper<DicBusinessItemDo> item = new LambdaQueryWrapper<>();
        item.eq(DicBusinessItemDo::getDicBusinessId, DicBusinessConstant.trackCompany).or();
        item.eq(DicBusinessItemDo::getDicBusinessId, DicBusinessConstant.fbaOrderOutPutTruck);
        List<DicBusinessItemDo> shopDoList = dicBusinessItemDao.selectList(item);
        return shopDoList.stream().collect(Collectors.toMap(DicBusinessItemDo::getId, shopDo -> shopDo));
    }


    private Map<Long, String> getShopIdMap() {
        List<ShopDo> shopDoList = shopDao.selectList(null);
        return shopDoList.stream().collect(Collectors.toMap(ShopDo::getId, ShopDo::getShopName));
    }

    private Map<Long, String> getStorageMap() {
        List<StorageDo> shopDoList = storageDao.selectList(null);
        return shopDoList.stream().collect(Collectors.toMap(StorageDo::getId, StorageDo::getStorageName));
    }


    private OrderDoorExportVo handlerOrderDoor(CostRecordDo costRecordDo) {

        OrderDoorPickDo et = orderDoorPickDao.selectById(costRecordDo.getThirdId());
        if (et == null) {
            return null;
        }
        OrderDoorExportVo exportVo = new OrderDoorExportVo();
        exportVo.setDoorNo(et.getDoorNo());
        exportVo.setOrderFee(et.getOrderFee());
        exportVo.setTrackNumber(et.getTrackNumber());
        exportVo.setTracKName(StaticDict.Track_Type.getText(et.getTrackId()));
        exportVo.setBookDate(et.getBookDate());
        exportVo.setBookHour(et.getBookHour());
        exportVo.setTips(et.getTips());
        ShopDo shopDo = shopDao.selectById(et.getShopId());
        if (shopDo != null) {
            exportVo.setShopName(shopDo.getShopName());
        }
        StorageDo storageDo = storageDao.selectById(et.getStorageId());
        if (storageDo != null) {
            exportVo.setStorageName(storageDo.getStorageName());
        }
        return exportVo;
    }

    private List<OrderOutputVo> handlerOrderOutPutNew(List<String> outIdCollect, Map<String, Double> pjIdMaps, List<String> fbaIdCollect) {
        Map<Integer, String> typeMap = getTypeMap();
        Map<Long, String> shopIdMap = getShopIdMap();
        Map<Long, String> storageMap = getStorageMap();
        List<OrderOutputVo> excelExportVos = new ArrayList<>();
        List<OrderOutputExportDto> orderOutputDoList = orderOutputDao.selectExportListByIds(outIdCollect);
        List<ProductInfoDo> productInfoDoList = productInfoDao.selectOutProductIdsByOrderIds(outIdCollect);
        List<OrderOutputExportDto> replaceOutDoList = replacementDao.selectOrderCostExcelExport(pjIdMaps.keySet().stream().collect(Collectors.toList()));
        if (replaceOutDoList != null) {
            orderOutputDoList.addAll(replaceOutDoList);
        }
        List<OrderOutputExportDto> fbaOutDoList = fbaOrderOutputDao.selectOrderCostExcelExport(fbaIdCollect);
        if (fbaOutDoList != null) {
            orderOutputDoList.addAll(fbaOutDoList);
        }
        String lastOrderNumber = "";
        Long lastPackageId = 0L;
        Long lastPackageDetailId = 0L;
        if (orderOutputDoList != null && orderOutputDoList.size() > 0) {
            if (productInfoDoList != null) {
                Map<Long, ProductInfoDo> productInfoDoMap = productInfoDoList.stream().collect(Collectors.toMap(ProductInfoDo::getId, productInfoDo -> productInfoDo));
                for (OrderOutputExportDto orderOutputExportDto : orderOutputDoList) {
                    if (!lastOrderNumber.equals(orderOutputExportDto.getOrderOutputNumber())) {
                        lastOrderNumber = orderOutputExportDto.getOrderOutputNumber();
                        OrderOutputVo outExcelExportVo = BeanConvert.INSTANCE.outputDto2ExportVo(orderOutputExportDto);
                        outExcelExportVo.setShopName(shopIdMap.get(orderOutputExportDto.getShopId()));
                        outExcelExportVo.setStorageName(storageMap.get(orderOutputExportDto.getStorageId()));
                        if (orderOutputExportDto.getTotalFee()==null||orderOutputExportDto.getTotalFee()==0){
                            outExcelExportVo.setTotalFee(pjIdMaps.get(orderOutputExportDto.getId()+""));
                        }
                        String trackName = "佐川即便";
                        if (orderOutputExportDto.getTrackId() != null) {
                            if (getTracIdMap().get(orderOutputExportDto.getTrackId())!=null)
                            trackName = StaticDict.Track_Type.getText(getTracIdMap().get(orderOutputExportDto.getTrackId()).getParentId());
                        }
                        outExcelExportVo.setTrackName(trackName);
                        outExcelExportVo.setOrderNo1(getMainOrderNo(orderOutputExportDto.getOrderNo()));
                        excelExportVos.add(outExcelExportVo);
                    }
                    OrderOutputVo orderOutputVo = excelExportVos.get(excelExportVos.size() - 1);
                    if (orderOutputVo.getPackageVoList() == null) {
                        orderOutputVo.setPackageVoList(new ArrayList<>());
                    }
                    if (orderOutputExportDto.getPackageId() == null) {
                        orderOutputVo.getPackageVoList().add(new OrderOutPackageVo());
                    } else if (!lastPackageId.equals(orderOutputExportDto.getPackageId())) {
                        lastPackageId = orderOutputExportDto.getPackageId();
                        OrderOutPackageVo orderOutPackageVo = BeanConvert.INSTANCE.orderCostDto2PackageExportVo(orderOutputExportDto);
                        orderOutputVo.getPackageVoList().add(orderOutPackageVo);
                    }

                    OrderOutPackageVo orderOutPackageVo = orderOutputVo.getPackageVoList().get(orderOutputVo.getPackageVoList().size() - 1);
                    if (orderOutPackageVo.getProductVoList() == null) {
                        orderOutPackageVo.setProductVoList(new ArrayList<>());
                    }
                    if (StringUtils.isBlank(orderOutputExportDto.getInSkuId())) {
                        //fba没有inSkuId
                        OrderOutProductVo orderOutProductVo = new OrderOutProductVo();
                        orderOutProductVo.setCommoditySku(orderOutputExportDto.getCommoditySku());
                        orderOutProductVo.setWeight(orderOutputExportDto.getSkuWeight());
                        orderOutProductVo.setHeight(orderOutputExportDto.getSkuHeight());
                        orderOutProductVo.setWidth(orderOutputExportDto.getSkuWidth());
                        orderOutProductVo.setLength(orderOutputExportDto.getSkuLength());
                        orderOutProductVo.setCount(orderOutputExportDto.getCount());
                        orderOutPackageVo.getProductVoList().add(orderOutProductVo);
                    } else {
                        String[] split = orderOutputExportDto.getInSkuId().split(",");
                        Map<String, Integer> skuCountMap = Arrays.stream(split).filter(StringUtils::isNotBlank).collect(Collectors.toMap(s -> s, s -> 1, Integer::sum));
                        for (String skuId : skuCountMap.keySet()) {

                            ProductInfoDo productInfoDo = productInfoDoMap.get(Long.parseLong(skuId));
                            if (productInfoDo == null) {
                                continue;
                            }
                            OrderOutProductVo orderOutProductVo = BeanConvert.INSTANCE.orderCostDto2ProductExportVo(productInfoDo);
                            orderOutProductVo.setCount(skuCountMap.get(skuId));
                            orderOutPackageVo.getProductVoList().add(orderOutProductVo);
                        }
                    }
                }
            }
        }
//        if (replaceOutDoList != null && replaceOutDoList.size() > 0) {
//            for (OrderOutputExportDto orderOutputExportDto : replaceOutDoList) {
//                if (!lastOrderNumber.equals(orderOutputExportDto.getOrderOutputNumber())) {
//                    lastOrderNumber = orderOutputExportDto.getOrderOutputNumber();
//                    OrderOutputVo outExcelExportVo = BeanConvert.INSTANCE.outputDto2ExportVo(orderOutputExportDto);
//                    excelExportVos.add(outExcelExportVo);
//                    if (outExcelExportVo.getPackageVoList() == null) {
//                        outExcelExportVo.setPackageVoList(new ArrayList<>());
//                    }
//                    outExcelExportVo.getPackageVoList().add(new OrderOutPackageVo());
//                }
//
//                OrderOutputVo orderOutputVo = excelExportVos.get(excelExportVos.size() - 1);
//                OrderOutPackageVo packageVo = orderOutputVo.getPackageVoList().get(0);
//                if (packageVo.getProductVoList() == null) {
//                    packageVo.setProductVoList(new ArrayList<>());
//                }
//                OrderOutProductVo orderOutProductVo = new OrderOutProductVo();
//                orderOutProductVo.setCommoditySku(orderOutputExportDto.getCommoditySku());
//                orderOutProductVo.setCount(orderOutputExportDto.getCount());
//                packageVo.getProductVoList().add(orderOutProductVo);
//            }
//        }
//        if (fbaIdCollect != null && fbaIdCollect.size() > 0) {
//
//        }
        return excelExportVos;
    }

    private String getMainOrderNo(String orderNo) {
        if (StringUtils.isBlank(orderNo)) {
            return orderNo;
        }
        if (orderNo.contains("-")) {
            String[] noStrs = orderNo.split("-");
            if (noStrs.length <= 3) {
                return (orderNo);
            } else {
                return orderNo.substring(0, orderNo.lastIndexOf("-"));
            }
        } else {
            return orderNo;
        }
    }

    public HeaderBillExportVo handlerHeaderFee(Long billId, CostRecordPageRes costRecordRes) {
        ManageBillDo billDo = manageBillDao.selectById(billId);
        if (billDo == null) {
            return null;
        }
        List<HeaderContainerDo> containerDoList = manageBillDao.getContainerNumber(billId);

        HeaderBillExportVo exportVo = new HeaderBillExportVo();
        exportVo.setBillLadingNumber(StringUtils.join(containerDoList.stream().map(HeaderContainerDo::getBillLadingNumber).collect(Collectors.toList()), ","));
        exportVo.setContainerNumber(StringUtils.join(containerDoList.stream().map(HeaderContainerDo::getContainerNumber).collect(Collectors.toList()), ","));
        exportVo.setFilePath(fileService.getFilePath(billDo.getFileId()));
        exportVo.setBillNo(billDo.getBillNo());
        costRecordRes.setContent(billDo.getBillNo());
        List<ManageHeaderPriceItemDo> itemDoList = manageHeaderPriceItemDao.selectList(new LambdaQueryWrapper<ManageHeaderPriceItemDo>().eq(ManageHeaderPriceItemDo::getBillId, billId).eq(ManageHeaderPriceItemDo::getType, "beginBill"));

        List<HeaderBillPriceItemExportVo> itemExportVoList = new ArrayList<>();
        for (ManageHeaderPriceItemDo itemDo : itemDoList) {
            HeaderBillPriceItemExportVo itemExportVo = new HeaderBillPriceItemExportVo();
            if (itemDo.getProjectId() != null) {
                StorageServiceProjectDo projectDo = storageServiceProjectDao.selectById(itemDo.getProjectId());
                if (projectDo == null) {
                    continue;
                }
                itemExportVo.setProjectName(projectDo.getProjectName());
            } else {
                itemExportVo.setProjectName(itemDo.getValue());
            }
            itemExportVo.setCurrencyType(itemDo.getCurrencyType());
            itemExportVo.setPrice(itemDo.getPrice());
            itemExportVo.setTax(itemDo.getTax());
            itemExportVoList.add(itemExportVo);
        }
        exportVo.setList(itemExportVoList);
        return exportVo;
    }


    private OrderBackExcelExportVo handlerBackOrder(CostRecordDo costRecordDo, CostRecordPageRes costRecordRes) {
        OrderBackExcelExportVo excelExportVo = new OrderBackExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        excelExportVo.setAfterAmount(costRecordDo.getAfterAmount());
        excelExportVo.setAccountType(costRecordDo.getAccountType());
        OrderBackDo orderBackDo = orderBackDao.selectById(costRecordDo.getThirdId());
        if (orderBackDo == null) {
            return null;
        }
        costRecordRes.setId(orderBackDo.getOrderOutNumber());
        OrderBackVo orderOutputVo = new OrderBackVo();
        ShopDo shopDo = shopDao.selectById(orderBackDo.getShopId());
        if (shopDo != null) {
            orderOutputVo.setShopName(shopDo.getShopName());
        }
        StorageDo storageDo = storageDao.selectById(orderBackDo.getStorageId());
        if (storageDo != null) {
            orderOutputVo.setStorageName(storageDo.getStorageName());
        }
        orderOutputVo.setShipFee(orderBackDo.getShipFee());
        orderOutputVo.setOperateFee(orderBackDo.getOperateFee());
        orderOutputVo.setDropFee(orderBackDo.getDropFee());
        orderOutputVo.setTaxFee(orderBackDo.getTaxFee());
        orderOutputVo.setOrderOutputNumber(orderBackDo.getOrderOutNumber());
        DicBusinessItemDo businessItemDo = dicBusinessItemDao.selectById(orderBackDo.getBackResult());
        if (businessItemDo != null) {
            orderOutputVo.setResult(businessItemDo.getDicItemValue());
        }
        DicBusinessItemDo businessItemDo1 = dicBusinessItemDao.selectById(orderBackDo.getStatus());
        if (businessItemDo1 != null) {
            orderOutputVo.setStatus(businessItemDo1.getDicItemValue());
        }
        orderOutputVo.setTrackNumber(orderBackDo.getTrackNumber());
        orderOutputVo.setTrackNanme(orderBackDo.getTrackName());
        LambdaQueryWrapper<OrderBackProductDo> productDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productDoLambdaQueryWrapper.eq(OrderBackProductDo::getOrderId, orderBackDo.getId());
        List<OrderBackProductDo> productDoList = orderBackProductDao.selectList(productDoLambdaQueryWrapper);
        StringBuilder sb = new StringBuilder();
        int count = 0;
        if (productDoList != null && productDoList.size() > 0) {
            for (OrderBackProductDo orderBackProductDo : productDoList) {
                ProductInfoDo productInfoDo = productInfoDao.selectById(orderBackProductDo.getProductId());
                if (productInfoDo == null) {
                    continue;
                }
                sb.append(productInfoDo.getCommoditySku());
                if (orderBackProductDo.getCount() == null) {
                    orderBackProductDo.setCount(1);
                }
                count = count + orderBackProductDo.getCount();
            }
        }
        orderOutputVo.setSku(sb.toString());
        orderOutputVo.setSkuCount(count);
        excelExportVo.setOrderBackVoList(orderOutputVo);
        return excelExportVo;
    }

    private OrderOutExcelExportVo handlerFbaOrderOutPut(CostRecordDo costRecordDo, CostRecordPageRes costRecordRes) {
        OrderOutExcelExportVo excelExportVo = new OrderOutExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        excelExportVo.setAfterAmount(costRecordDo.getAfterAmount());
        excelExportVo.setAccountType(costRecordDo.getAccountType());
        FbaOrderOutputDo orderOutputDo = fbaOrderOutputDao.selectById(costRecordDo.getThirdId());
        if (orderOutputDo == null) {
            return null;
        }

        //出库单
        costRecordRes.setId(orderOutputDo.getFbaOrderOutputNumber());
        List<OrderOutputVo> orderOutputVoList = new ArrayList<>();
        OrderOutputVo orderOutputVo = new OrderOutputVo();
        ShopDo shopDo = shopDao.selectById(orderOutputDo.getShopId());
        if (shopDo != null) {
            orderOutputVo.setShopName(shopDo.getShopName());
        }
        StorageDo storageDo = storageDao.selectById(orderOutputDo.getStorageId());
        if (storageDo != null) {
            orderOutputVo.setStorageName(storageDo.getStorageName());
        }
        orderOutputVo.setTotalFee(orderOutputDo.getShipFee() + orderOutputDo.getOperateFee() + orderOutputDo.getOutFee() + orderOutputDo.getTaxFee());
        orderOutputVo.setShipFee(orderOutputDo.getShipFee());
        orderOutputVo.setOperateFee(orderOutputDo.getOperateFee());
        orderOutputVo.setTaxFee(orderOutputDo.getTaxFee());
        orderOutputVo.setOutFee(orderOutputDo.getOutFee());
        orderOutputVo.setOrderOutputNumber(orderOutputDo.getFbaOrderOutputNumber());
        orderOutputVo.setOrderNo(orderOutputDo.getOrderNo());
        orderOutputVo.setTrackNumber(orderOutputDo.getTrackNumber());
        DicBusinessItemDo businessItemDo1 = dicBusinessItemDao.selectById(orderOutputDo.getTrackId());
        if (businessItemDo1 != null) {
            orderOutputVo.setTrackName(businessItemDo1.getDicItemValue());
        }
        FbaStorageDo fbaStorageDo = fbaStorageDao.selectById(orderOutputDo.getFbaStorageId());
        if (fbaStorageDo != null) {
            orderOutputVo.setAddress(fbaStorageDo.getFbaCode() + " " + fbaStorageDo.getFbaName());
        }
        orderOutputVo.setSendTime(orderOutputDo.getSendTime());

        List<OrderOutPackageVo> packageVoList = new ArrayList<>();
        LambdaQueryWrapper<FbaOrderOutputPackageDo> packageDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        packageDoLambdaQueryWrapper.eq(FbaOrderOutputPackageDo::getFbaOrderOutputId, orderOutputDo.getId());
        List<FbaOrderOutputPackageDo> packageDoList = fbaOrderOutputPackageDao.selectList(packageDoLambdaQueryWrapper);
        if (!CollectionUtils.isEmpty(packageDoList)) {
            for (FbaOrderOutputPackageDo packageDo : packageDoList) {
                OrderOutPackageVo orderOutPackageVo = new OrderOutPackageVo();
                try {
                    orderOutPackageVo.setHeight(packageDo.getHeight());
                    orderOutPackageVo.setWidth(packageDo.getWidth());
                    orderOutPackageVo.setLength(packageDo.getLength());
                } catch (Exception e) {
//                    orderOutPackageVo.setVolume(0D);
                }
                orderOutPackageVo.setWeight(packageDo.getWeight());
                List<OrderOutProductVo> productVoList = new ArrayList<>();
                LambdaQueryWrapper<FbaOrderOutputProductDo> productDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                productDoLambdaQueryWrapper.eq(FbaOrderOutputProductDo::getPackageId, packageDo.getId());
                List<FbaOrderOutputProductDo> orderOutputProductDoList = fbaOrderOutputProductDao.selectList(productDoLambdaQueryWrapper);
                if (!CollectionUtils.isEmpty(orderOutputProductDoList)) {
                    for (FbaOrderOutputProductDo orderOutputProductDo : orderOutputProductDoList) {
                        OrderOutProductVo orderOutProductVo = new OrderOutProductVo();
                        orderOutProductVo.setCount(orderOutputProductDo.getCount());
                        ProductInfoDo productInfoDo = productInfoDao.selectById(orderOutputProductDo.getProductId());
                        if (productInfoDo != null) {
                            orderOutProductVo.setCommoditySku(productInfoDo.getCommoditySku());
                        } else {
                            orderOutProductVo.setCommoditySku("SKU不存在或被删除");
                        }
                        productVoList.add(orderOutProductVo);
                    }
                }

                orderOutPackageVo.setProductVoList(productVoList);
                packageVoList.add(orderOutPackageVo);
            }
        }
        orderOutputVo.setPackageVoList(packageVoList);
        orderOutputVoList.add(orderOutputVo);
        excelExportVo.setOrderOutputVoList(orderOutputVoList);

        return excelExportVo;
    }

    private OrderWorkExcelExportVo handlerWorkOrder(CostRecordDo costRecordDo, CostRecordPageRes costRecordRes) {
        OrderWorkExcelExportVo excelExportVo = new OrderWorkExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        excelExportVo.setAccountType(costRecordDo.getAccountType());
        excelExportVo.setAfterAmount(costRecordDo.getAfterAmount());
        OrderWorkDo orderWorkDo = orderWorkDao.selectById(costRecordDo.getThirdId());
        if (orderWorkDo == null) {
            return null;
        }
        costRecordRes.setId(orderWorkDo.getWorkNumber());
        List<OrderWorkVo> orderWorkVoList = new ArrayList<>();
        OrderWorkVo orderWorkVo = new OrderWorkVo();
        orderWorkVo.setWorkNumber(orderWorkDo.getWorkNumber());
        ShopDo shopDo = shopDao.selectById(orderWorkDo.getShopId());
        if (Objects.nonNull(shopDo)) {
            orderWorkVo.setShopName(shopDo.getShopName());
        }
        StorageDo storageDo = storageDao.selectById(orderWorkDo.getStorageId());
        if (storageDo != null) {
            orderWorkVo.setStorageName(storageDo.getStorageName());
        }
        DicBusinessItemDo businessItemDo = dicBusinessItemDao.selectById(orderWorkDo.getWorkType());
        if (businessItemDo != null) {
            orderWorkVo.setWorkType(businessItemDo.getDicItemValue());
        }

        LambdaQueryWrapper<OrderWorkPriceDo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderWorkPriceDo::getOrderWorkId, orderWorkDo.getId());
        List<OrderWorkPriceDo> workPriceDoList = orderWorkPriceDao.selectList(wrapper);
        if (!CollectionUtils.isEmpty(workPriceDoList)) {
            List<OrderWorkPriceVo> orderWorkPriceVoList = new ArrayList<>();
            for (OrderWorkPriceDo orderWorkPriceDo : workPriceDoList) {
                OrderWorkPriceVo orderWorkPriceVo = new OrderWorkPriceVo();
                orderWorkPriceVo.setName(orderWorkPriceDo.getName());
                orderWorkPriceVo.setPrice(orderWorkPriceDo.getPrice());
                orderWorkPriceVo.setType(orderWorkPriceDo.getCurrency().intValue());
                orderWorkPriceVoList.add(orderWorkPriceVo);
            }
            orderWorkVo.setOrderWorkPriceVoList(orderWorkPriceVoList);
        }

        orderWorkVoList.add(orderWorkVo);
        excelExportVo.setOrderWorkVoList(orderWorkVoList);
        return excelExportVo;
    }

    private StockFeeExcelExportVo handlerStockFee(CostRecordDo costRecordDo, CostRecordPageRes costRecordRes) {
        StockFeeExcelExportVo excelExportVo = new StockFeeExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        excelExportVo.setAfterAmount(costRecordDo.getAfterAmount());
        excelExportVo.setAccountType(costRecordDo.getAccountType());
        ShopDo shopDo = shopDao.selectById(costRecordDo.getShopId());
        if (shopDo != null) {
            excelExportVo.setShopName(shopDo.getShopName());
        }
        StockHistoryDo stockHistoryDo = stockHistoryDao.selectById(costRecordDo.getThirdId());
        costRecordRes.setId(costRecordDo.getContent());
        List<StockFeeVo> stockFeeVoList = new ArrayList<>();
        if (stockHistoryDo != null) {
            StockFeeVo stockFeeVo = new StockFeeVo();
            StorageDo storageDo = storageDao.selectById(stockHistoryDo.getStorageId());
            if (storageDo != null) {
                stockFeeVo.setStorageName(storageDo.getStorageName());
            }
            stockFeeVo.setCount(stockHistoryDo.getCanSell() + stockHistoryDo.getCanotSell());
            stockFeeVo.setCreateDate(stockHistoryDo.getCreateDate());
            stockFeeVo.setTotalVolume(stockHistoryDo.getTotalVolume());
            stockFeeVo.setV1(stockHistoryDo.getV1Volume());
            stockFeeVo.setV2(stockHistoryDo.getV2Volume());
            stockFeeVo.setV3(stockHistoryDo.getV3Volume());
            stockFeeVo.setV4(new BigDecimal(stockHistoryDo.getTotalVolume() - stockHistoryDo.getV1Volume() - stockHistoryDo.getV2Volume() - stockHistoryDo.getV3Volume()).setScale(2, RoundingMode.HALF_UP).doubleValue());
            stockFeeVoList.add(stockFeeVo);
        }
        excelExportVo.setStockFeeVoList(stockFeeVoList);
        return excelExportVo;
    }

    private OrderInputExcelExportVo handlerOrderInput(CostRecordDo costRecordDo, CostRecordPageRes costRecordRes) {

        OrderInputExcelExportVo excelExportVo = new OrderInputExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        excelExportVo.setAfterAmount(costRecordDo.getAfterAmount());
        excelExportVo.setAccountType(costRecordDo.getAccountType());
        OrderInputDo orderInputDo = orderInputDao.selectById(costRecordDo.getThirdId());
        if (orderInputDo == null) {
            return null;
        }
        costRecordRes.setId(orderInputDo.getOrderInputNumber());
        OrderInputVo orderInputVo = new OrderInputVo();
        orderInputVo.setCarriageTrackNo(orderInputDo.getCarriageTrackNo());
        orderInputVo.setClearanceFee(orderInputDo.getClearanceFee());
        orderInputVo.setInputFee(0F);//todo 去费用表里查
        orderInputVo.setOrderInputNumber(orderInputDo.getOrderInputNumber());
        orderInputVo.setSeaFee(orderInputDo.getSeaFee());
        orderInputVo.setSendType(orderInputDo.getSendType());
        orderInputVo.setServiceFee(orderInputDo.getServiceFee());
        orderInputVo.setTaxFee(orderInputDo.getTaxFee());
        orderInputVo.setVolume(orderInputDo.getVolume());
        List<InputMarkProduct> productList = orderInputService.getProductList(orderInputDo.getId());
        List<OrderInputSkuExport> orderInputSkuExportList =new ArrayList<>();
        for (InputMarkProduct inputMarkProduct : productList) {
            OrderInputSkuExport skuExport=BeanConvert.INSTANCE.inputMarkProduct2export(inputMarkProduct);
            skuExport.setVolume(new BigDecimal(inputMarkProduct.getLength()*inputMarkProduct.getWidth()*inputMarkProduct.getHeight()* inputMarkProduct.getCount()/1000000).setScale(2,RoundingMode.HALF_UP).doubleValue());
            skuExport.setInputFee(new BigDecimal(skuExport.getVolume()/orderInputVo.getVolume()*costRecordDo.getCostAmount()).setScale(2,RoundingMode.HALF_UP).doubleValue());
            orderInputSkuExportList.add(skuExport);
        }
        orderInputVo.setOrderInputSkuExportList(orderInputSkuExportList);
        List<OrderInputVo> orderInputVos = new ArrayList<>();
        orderInputVos.add(orderInputVo);
        excelExportVo.setOrderInputVoList(orderInputVos);

        return excelExportVo;
    }


    private OrderOutExcelExportVo handlerOrderOutPut(CostRecordDo costRecordDo, CostRecordPageRes costRecordRes) {
        OrderOutExcelExportVo excelExportVo = new OrderOutExcelExportVo();
        excelExportVo.setCreateTime(costRecordDo.getCreateTime());
        excelExportVo.setCostAmount(costRecordDo.getCostAmount());
        excelExportVo.setContent(costRecordDo.getContent());
        excelExportVo.setAfterAmount(costRecordDo.getAfterAmount());
        excelExportVo.setAccountType(costRecordDo.getAccountType());
        //出库单
        List<OrderOutputVo> orderOutputVoList = new ArrayList<>();
        if (costRecordDo.getContent().startsWith("PJ")) {
            ReplacementDo replacementDo = replacementDao.selectById(costRecordDo.getThirdId());
            if (replacementDo == null) {
                return null;
            }
            costRecordRes.setId(replacementDo.getOutputOrderNo());
            OrderOutputVo orderOutputVo = new OrderOutputVo();
            ShopDo shopDo = shopDao.selectById(replacementDo.getOutputShopId());
            if (shopDo != null) {
                orderOutputVo.setShopName(shopDo.getShopName());
            }
            StorageDo storageDo = storageDao.selectById(replacementDo.getOutputStorageId());
            if (storageDo != null) {
                orderOutputVo.setStorageName(storageDo.getStorageName());
            }
            orderOutputVo.setShipFee(costRecordDo.getCostAmount());
            orderOutputVo.setOrderOutputNumber(replacementDo.getOrderNo());
            orderOutputVo.setOrderNo(replacementDo.getOutputOrderNo());
            DicBusinessItemDo itemDo = dicBusinessItemDao.selectById(replacementDo.getTrackId());
            if (itemDo != null) {
                orderOutputVo.setTrackName(StaticDict.Track_Type.getText(itemDo.getParentId()));
            }
            orderOutputVo.setTrackNumber(replacementDo.getTrackNumber());
            orderOutputVo.setAddress(replacementDo.getOutputAddress());
            orderOutputVo.setSendTime(replacementDo.getUpdateTime());
            orderOutputVoList.add(orderOutputVo);
        } else {

            OrderOutputDo orderOutputDo = orderOutputDao.selectById(costRecordDo.getThirdId());
            if (orderOutputDo == null) {
                return null;
            }
            costRecordRes.setId(orderOutputDo.getOrderOutputNumber());
            OrderOutputVo orderOutputVo = new OrderOutputVo();
            ShopDo shopDo = shopDao.selectById(orderOutputDo.getShopId());
            if (shopDo != null) {
                orderOutputVo.setShopName(shopDo.getShopName());
            }
            StorageDo storageDo = storageDao.selectById(orderOutputDo.getStorageId());
            if (storageDo != null) {
                orderOutputVo.setStorageName(storageDo.getStorageName());
            }
            orderOutputVo.setTotalFee(orderOutputDo.getShipFee() + orderOutputDo.getOperateFee() + orderOutputDo.getOutFee() + orderOutputDo.getTaxFee() + orderOutputDo.getCutFee());
            orderOutputVo.setShipFee(orderOutputDo.getShipFee());
            orderOutputVo.setOperateFee(orderOutputDo.getOperateFee());
            orderOutputVo.setTaxFee(orderOutputDo.getTaxFee());
            orderOutputVo.setOutFee(orderOutputDo.getOutFee());
            orderOutputVo.setCutFee(orderOutputDo.getCutFee());
            orderOutputVo.setOrderOutputNumber(orderOutputDo.getOrderOutputNumber());
            orderOutputVo.setOrderNo(orderOutputDo.getOrderNo());
            orderOutputVo.setOrderNo1(getMainOrderNo(orderOutputDo.getOrderNo()));
            DicBusinessItemDo itemDo = dicBusinessItemDao.selectById(orderOutputDo.getTrackId());
            if (itemDo != null) {
                orderOutputVo.setTrackName(StaticDict.Track_Type.getText(itemDo.getParentId()));
            }
            orderOutputVo.setTrackNumber(orderOutputDo.getTrackNumber());
            orderOutputVo.setAddress(orderOutputDo.getArea());
            orderOutputVo.setSendTime(orderOutputDo.getSendTime());

            List<OrderOutPackageVo> packageVoList = new ArrayList<>();
            LambdaQueryWrapper<OrderOutputPackageDo> packageDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            packageDoLambdaQueryWrapper.eq(OrderOutputPackageDo::getOrderOutputId, orderOutputDo.getId());
            List<OrderOutputPackageDo> packageDoList = orderOutputPackageDao.selectList(packageDoLambdaQueryWrapper);
            if (!CollectionUtils.isEmpty(packageDoList)) {
                for (OrderOutputPackageDo packageDo : packageDoList) {
                    OrderOutPackageVo orderOutPackageVo = new OrderOutPackageVo();
                    try {
                        orderOutPackageVo.setHeight(packageDo.getHeight());
                        orderOutPackageVo.setWidth(packageDo.getWidth());
                        orderOutPackageVo.setLength(packageDo.getLength());
                    } catch (Exception e) {
//                    orderOutPackageVo.setVolume(0D);
                    }
                    orderOutPackageVo.setWeight(packageDo.getWeight());
                    List<OrderOutProductVo> productVoList = new ArrayList<>();
                    LambdaQueryWrapper<OrderOutputProductDo> productDoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    productDoLambdaQueryWrapper.eq(OrderOutputProductDo::getPackageId, packageDo.getId());
                    List<OrderOutputProductDo> orderOutputProductDoList = orderOutputProductDao.selectList(productDoLambdaQueryWrapper);
                    if (!CollectionUtils.isEmpty(orderOutputProductDoList)) {
                        for (OrderOutputProductDo orderOutputProductDo : orderOutputProductDoList) {
                            OrderOutProductVo orderOutProductVo = new OrderOutProductVo();
                            orderOutProductVo.setCount(orderOutputProductDo.getCount());

                            ProductInfoDo productInfoDo = productInfoDao.selectById(orderOutputProductDo.getProductId());
                            if (productInfoDo != null) {
                                orderOutProductVo.setHeight(productInfoDo.getHeight());
                                orderOutProductVo.setLength(productInfoDo.getLength());
                                orderOutProductVo.setWeight(productInfoDo.getWeight());
                                orderOutProductVo.setWidth(productInfoDo.getWidth());
                                orderOutProductVo.setCommoditySku(productInfoDo.getCommoditySku());
                            } else {
                                orderOutProductVo.setCommoditySku(orderOutputDo.getCommoditySku());
                            }
                            productVoList.add(orderOutProductVo);
                        }
                    }

                    orderOutPackageVo.setProductVoList(productVoList);
                    packageVoList.add(orderOutPackageVo);
                }
            }
            orderOutputVo.setPackageVoList(packageVoList);
            orderOutputVoList.add(orderOutputVo);
        }
        excelExportVo.setOrderOutputVoList(orderOutputVoList);
        return excelExportVo;
    }

    /**
     * 查询分页列表之后对列表数据的处理
     */
    private List<CostRecordListRes> handlerListAfter(List<CostRecordDo> list) {
        List<CostRecordListRes> result = new ArrayList<>();
        for (CostRecordDo costRecordDo : list) {
            CostRecordListRes costRecordRes = new CostRecordListRes();
            BeanUtils.copyProperties(costRecordDo, costRecordRes);
            costRecordRes.setType(getTypeMap().get(costRecordDo.getType()));
            costRecordRes.setStatus(costRecordDo.getStatus() == null || 0 == costRecordDo.getStatus() ? "未入账" : "已扣款");
            if (costRecordDo.getAccountType() == null) {
                costRecordDo.setAccountType(1);
            }
            costRecordRes.setAccountType(costRecordDo.getAccountType() == 0L ? "人民币账户" : "日币账户");
            ShopDo shopDo = shopDao.selectById(costRecordDo.getShopId());
            if (shopDo != null) {
                costRecordRes.setShop(shopDo.getShopName());
            }
            result.add(costRecordRes);
        }
        return result;
    }

    private Map<Integer, String> getTypeMap() {
        Map<Integer, String> typeMap = new HashMap<>();
        typeMap.put(0, "出库费");
        typeMap.put(1, "入库费");
        typeMap.put(2, "仓储费");
        typeMap.put(3, "定制费");
        typeMap.put(4, "退货费");
        typeMap.put(5, "FBA转运");
        typeMap.put(6, "头程运费");
        typeMap.put(7, "维修订单");
        typeMap.put(8, "上门取件");
        typeMap.put(9, "废弃费");
        return typeMap;
    }

    private IPage<CostRecordDo> pageInit(Map<String, String> map) {
        IPage<CostRecordDo> page = new Page<>();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<CostRecordDo> queryBuild(Map<String, String> map) {
        QueryWrapper<CostRecordDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("companyId"))) {
            wrapper.eq("company_id", map.get("companyId"));
        }
        if (StringUtils.isNotBlank(map.get("dateType")) && "payTime".equals(map.get("dateType"))) {
            if (StringUtils.isNotBlank(map.get("dateStart"))) {
                wrapper.ge("pay_time", map.get("dateStart"));
            }
            if (StringUtils.isNotBlank(map.get("dateEnd"))) {
                wrapper.lt("pay_time", TimeUtils.addDaysFormat(map.get("dateEnd"), "yyyy-MM-dd", 1));
            }
            wrapper.orderByDesc("pay_time", "id");

        } else {
            if (StringUtils.isNotBlank(map.get("dateStart"))) {
                wrapper.ge("create_time", map.get("dateStart"));
            }
            if (StringUtils.isNotBlank(map.get("dateEnd"))) {
                wrapper.lt("create_time", TimeUtils.addDaysFormat(map.get("dateEnd"), "yyyy-MM-dd", 1));
            }
            wrapper.orderByDesc("create_time", "id");
        }
        if (StringUtils.isNotBlank(map.get("type"))) {
            wrapper.eq("type", map.get("type"));
        }
        if (StringUtils.isNotBlank(map.get("shopId"))) {
            wrapper.eq("shop_id", map.get("shopId"));
        }
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.like("content", map.get("keyword"));
        }
        return wrapper;
    }


}
